$(function () {

    $('#save-banner-btn').click(function (event) {
        event.preventDefault()
        var self = $(this);
        var dialog = $('#banner-dialog')
        var name_input = $("input[name='name']")
        var image_url_input = $("input[name='image_url']")
        var link_url_input = $("input[name='link_url']")
        var priority_input = $("input[name='priority']")

        var name = name_input.val();
        var image_url = image_url_input.val();
        var link_url = link_url_input.val();
        var priority = priority_input.val();
        var submit_type = self.attr('data-type');
        var banner_id = self.attr('data-id');

        if (!name || !image_url || !link_url || !priority) {
            zlalert.alertInfoToast("请正确输入数据");
            return;
        }
        var url = '';
        if (submit_type == 'update') {
            url = '/cms/update_banner/';
        } else {
            url = '/cms/add_banners/';
        }

        zlajax.post({
            'url': url,
            'data': {
                'name': name,
                'image_url': image_url,
                'link_url': link_url,
                'priority': priority,
                'banner_id': banner_id
            },
            'success': function (data) {
                if (data['code'] == 200) {
                    dialog.modal('hide');
                    window.location.reload();
                    zlalert.alertSuccessToast(data['message'])
                } else {
                    zlalert.alertInfo(data['message']);
                }
            }
        })
    });
});

$(function () {
    $('.edit-banner-btn').click(function (event) {
        var self = $(this);
        event.preventDefault();
        var dialog = $('#banner-dialog');
        dialog.modal('show');
        var tr = self.parent().parent();
        var name = tr.attr('data-name');
        var image_url = tr.attr('data-image');
        var link_url = tr.attr('data-link');
        var priority = tr.attr('data-priority');


        var name_input = dialog.find("input[name='name']");
        var image_url_input = dialog.find("input[name='image_url']");
        var link_url_input = dialog.find("input[name='link_url']");
        var priority_input = dialog.find("input[name='priority']");
        var save_btn = dialog.find('#save-banner-btn');


        name_input.val(name);
        image_url_input.val(image_url);
        link_url_input.val(link_url);
        priority_input.val(priority);
        save_btn.attr('data-type', 'update');
        save_btn.attr('data-id', tr.attr('data-id'));
    });
});


$(function () {
    $('.delete-banner-btn').click(function (event) {
        event.preventDefault();
        var self = $(this);
        var banner_id = self.parent().parent().attr('data-id');
        zlalert.alertConfirm({
            'msg': '您确定要删除这个轮播图',
            'confirmCallback': function () {
                zlajax.post({
                    'url': '/cms/delete_banner/',
                    'data': {
                        'banner_id': banner_id
                    },
                    'success': function (data) {
                        if (data['code'] == 200) {
                            zlalert.alertSuccessToast('删除成功');
                            window.location.reload();
                        } else {
                            zlalert.alertInfoToast(data['message'])
                        }
                    }
                })
            },
            'cancelCallback': function () {

            }
        })
    });
});

$(function () {
    var domain = 'http://p9f6b6xz5.bkt.clouddn.com/'
    zlqiniu.setUp({
        'domain': domain,
        'browse_btn': 'upload-btn',
        'uptoken_url': '/cms/uptoken/',
        'success': function (up, file, info) {
            console.log(file);
            var imageInput = $("input[name='image_url']");
            imageInput.val(file.name);

        }
    });
});